package OSMS.action.goodsfunc;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import OSMS.dao.GoodsDao;
import OSMS.entity.Customers;
import OSMS.entity.Goods;
/*
 * 商品详情页面
 * 	业务逻辑如下：
 * 	1、通过request.getParameter("cid")首先从商品详情页面（detailPage.jsp）中获取用户编号cid并转化为int型
 * 	2、通过调用GoodsDao中的getCarById（cid）：来得到一个装有购物车中所有数据的列表carList
 * 	3、将该列表装到域对象request中
 * 	4、请求转发到showCar.jsp页面显示购物车详细信息
 */
public class goodsDetailServlet extends HttpServlet{

	private GoodsDao dao = new GoodsDao();
	 
	
	
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//---------------模拟用户登录、商品展示页点击--------------------------//
		HttpSession session = request.getSession();
		
		Customers cus = (Customers)session.getAttribute("cus");
		String str_gid = request.getParameter("gid");
		
//		System.out.println(str_gid);
		int gid = Integer.parseInt(str_gid);
		Goods good = new Goods();
		try {
			good = dao.getGoodsById(gid);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		System.out.println(good.getPicture());
		
//---点击进入商品首页后，该商品的浏览量+1
		try {
			boolean flag_add_search = dao.updateSearchNum(gid);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
//---点击进入商品首页后，该用户对该商品类型的搜索量+1
		boolean flag_add_visit = false;
		try {
			flag_add_visit = dao.updateVisitNum(cus.getCid(),Integer.parseInt(good.getGtype()));
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		try {
			// 1. 调用GoodsDao的方法通过点击具体的商品来查询goods数据
			List<Goods> gList = dao.searchGoodsByGid(gid);
			if(gList.isEmpty()){
				 System.out.println("空的！！！");
			}
			// 2. 将list数据装入session对象中。
	       
	        session.setAttribute("gList", gList);
			// 3. 选择对应的跳转方式（请求转发）跳转到商品详情detailPage.jsp页面.
			request.setCharacterEncoding("utf-8");
//			request.getRequestDispatcher("detailPage.jsp").forward(request, response);
			response.sendRedirect(request.getServletContext().getContextPath() + "/jsp/goods/detailPage.jsp");
		} catch(Exception ex) {
			ex.printStackTrace();
		}
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}
	
	

}
